package com.yxx.sys.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yxx.sys.common.DataGridView;
import com.yxx.sys.common.Result;
import com.yxx.sys.common.WebUtils;
import com.yxx.sys.domain.AppUser;
import com.yxx.sys.domain.LoginInfo;
import com.yxx.sys.domain.User;
import com.yxx.sys.service.AppUserService;
import com.yxx.sys.service.LoginInfoService;
import com.yxx.sys.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Map;

/**
 * @Author: YuXinXin
 * @Description:
 * @Date:Created in 21:27 2022/5/13
 **/
@RestController
  @RequestMapping("/login")
  @Api("pc端登录控制器")
  public class LoginController {

    @Autowired
  LoginInfoService loginInfoService;

  @Autowired
  UserService userService;

  @Autowired
  AppUserService appUserService;

  @PostMapping("/login")
  @ApiOperation("登录")
  public DataGridView login(String loginname, String pwd, String vername){
    //验证码
    LineCaptcha captcha = (LineCaptcha)WebUtils.getSession().getAttribute("cap");
    //判断验证码是否输入正确
    boolean verify = captcha.verify(vername);
    if(!verify)
      return new DataGridView(-1, "验证码错误",null);

    QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("login_name",loginname).eq("pwd", SecureUtil.md5(pwd));
    User user = userService.getOne(queryWrapper);
    if(BeanUtil.isEmpty(user))
      return new DataGridView(-1, "用户名或者密码错误",null);
    //添加登录日志
    LoginInfo entity = new LoginInfo();
    entity.setLoginName(user.getLoginName());
    entity.setLoginIp(WebUtils.getRequest().getRemoteAddr());//ip
    entity.setLoginTime(LocalDateTime.now());
    loginInfoService.save(entity);
    Map<String, String> map = MapUtil.newHashMap();
    map.put("loginName",user.getLoginName());
    map.put("imgPath",user.getImgPath());
    return new DataGridView(200, "登陆成功",map);
  }


  //验证码图片
  @GetMapping("/getImage")
  @ApiOperation("获得验证码图片")
  public String ImgCode() {
    LineCaptcha captcha = CaptchaUtil.createLineCaptcha(150, 50, 4, 6);
    ByteArrayInputStream imageStream = new ByteArrayInputStream(captcha.getImageBytes());
    WebUtils.getSession().setAttribute("cap", captcha);
    //将验证码写进浏览器
    ServletOutputStream outputStream = null;

    try {
      outputStream = WebUtils.getResponse().getOutputStream();
    } catch (IOException e) {
      e.printStackTrace();
    }
    captcha.write(outputStream);
    return null;
  }



  @PostMapping("/appLogin")
  @ApiOperation("app登录")
  public DataGridView login(String uid, String passwd){
    QueryWrapper<AppUser> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("uid",uid).eq("passwd", SecureUtil.md5(passwd));
    AppUser appUser = appUserService.getOne(queryWrapper);
    if(BeanUtil.isEmpty(appUser))
    {
      return new DataGridView(-1, "用户名或者密码错误",null);
    }
    Map<String, String> map = MapUtil.newHashMap();
    map.put("name",appUser.getName());
    return new DataGridView(200, "登陆成功",map);
  }

  }
